package com.block.common.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.block.common.config.AliIPConfig;
import com.block.common.model.dto.AppInfoDTO;
import com.block.common.util.ALiIpUtil;
import com.block.db.entity.AppEncrypt;
import com.block.db.entity.ApplicationInfo;
import com.block.db.mapper.ApplicationInfoMapper;
import com.block.common.service.IApplicationInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 包信息管理 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-11-14
 */
@Service
public class ApplicationInfoServiceImpl extends ServiceImpl<ApplicationInfoMapper, ApplicationInfo> implements IApplicationInfoService {

    @Autowired
    AliIPConfig aliIPConfig;

    @Resource
    private RedisTemplate<String, ApplicationInfo> redisApplicationInfoTemplate;

    private  static  final  String APPINFO_KEY_PREFIX="APPINFO:APPTYPE:";
    private  static  final  Long APPINFO_KEY_TIME_OUT=4L;

    @Override
    public ApplicationInfo getAppInfoByType(String appType){
        String redisKey = APPINFO_KEY_PREFIX+appType;
        ApplicationInfo applicationInfo= redisApplicationInfoTemplate.opsForValue().get(redisKey);
        if(ObjectUtil.isEmpty(applicationInfo)){
            applicationInfo = this.baseMapper.getAppInfoByType(appType);
            if(applicationInfo!=null) {
                redisApplicationInfoTemplate.opsForValue().set(redisKey, applicationInfo, APPINFO_KEY_TIME_OUT, TimeUnit.HOURS);
            }
        }
        return applicationInfo;
    }

    /**
     * 获取处理后的地址信息
     */
    @Override
    public AppInfoDTO getDealAfterUrl(String reqIp, ApplicationInfo applicationInfo){

        AppInfoDTO appInfoDTO = new AppInfoDTO();

        Integer dealFlag = applicationInfo.getDealFlag();
        if (ObjectUtil.equal(dealFlag, Integer.valueOf(0)) ||
                StrUtil.isEmpty(applicationInfo.getOriginUrl()) ||
                StrUtil.isEmpty(applicationInfo.getAddrInfo())){
            // 不进行特殊处理，或者处理之后返回的地址信息为空，或者要处理的地址信息为空
            // 则返回
            appInfoDTO.setSelfFlag(true);
            appInfoDTO.setReqUrl(applicationInfo.getH5Url());
            return appInfoDTO;
        }

        List<String> addressList = JSON.parseArray(applicationInfo.getAddrInfo(), String.class);
        if (CollUtil.isEmpty(addressList)){
            // 处理的地址信息为空
            appInfoDTO.setSelfFlag(true);
            appInfoDTO.setReqUrl(applicationInfo.getH5Url());
            return appInfoDTO;
        }

        boolean areaIP = ALiIpUtil.isTargetAreaIP(reqIp, addressList, aliIPConfig.getAppCode());
        if (areaIP){
            // 如果是目标地址，则返回原信息
            appInfoDTO.setSelfFlag(false);
            appInfoDTO.setReqUrl(applicationInfo.getOriginUrl());
            return appInfoDTO;
        }

        appInfoDTO.setSelfFlag(true);
        appInfoDTO.setReqUrl(applicationInfo.getH5Url());
        return appInfoDTO;
    }

}
